#include <ESP8266WiFi.h>    //WIFI库，配网必需
#include <ESP8266WebServer.h> //http server

#define RELAY_PIN 0

ESP8266WebServer server(80);

/* 微信智能配网 */
void smartConfig()
{
  WiFi.mode(WIFI_STA);//设置STA模式
  Serial.println("\r\nWait for Smartconfig...");//打印log信息
  WiFi.beginSmartConfig();//开始SmartConfig，等待手机端发出用户名和密码
  while(1)
  {
    Serial.println(".");
    digitalWrite(LED_BUILTIN,HIGH);//指示灯闪烁
    delay(1000);
    digitalWrite(LED_BUILTIN,LOW);//指示灯闪烁
    delay(1000);
    if(WiFi.smartConfigDone())//配网成功，接收到SSID和密码
    {
      WiFi.setAutoConnect(true);  // 设置自动连接
      WiFi.setAutoReconnect(true);
      Serial.println("SmartConfig Success");
      Serial.printf("SSID:%s\r\n", WiFi.SSID().c_str());
      Serial.printf("PSW:%s\r\n", WiFi.psk().c_str());
      break;      
    }
  }  
}

/*连接网络*/
void blinkerConnect()
{
    int count = 0;     //时间计数
    bool WIFI_Status = true;   //WIFI状态标志位
	Serial.println("\r\n正在连接WIFI...");
    WiFi.begin();//自动重连
    while(WiFi.status()!=WL_CONNECTED)//判断是否连接WIFI成功
    {
      if(WIFI_Status)
      {
          Serial.print(".");
          digitalWrite(LED_BUILTIN, HIGH);  
          delay(500);                       
          digitalWrite(LED_BUILTIN, LOW);    
          delay(500);                 
          count++;
          if(count>=30)//5s
          {
              WIFI_Status = false;
              Serial.println("WiFi连接失败，请用手机进行配网"); 
          }        
        }
        else
        {
          smartConfig();  //微信智能配网
        }
    }

    Serial.println("连接成功");  
    Serial.print("IP:");
    Serial.println(WiFi.localIP());
}

void setup() {
   
    // 初始化有LED  LED_BUILTIN 为开发板的指示灯管脚
    pinMode(LED_BUILTIN, OUTPUT);
    digitalWrite(LED_BUILTIN, HIGH);
    pinMode(RELAY_PIN, OUTPUT);
    digitalWrite(RELAY_PIN, LOW);
    Serial.begin(115200);
	Serial.println("\r\nBoot");
	//网络连接
	blinkerConnect();

    //http server
   server.begin();                   
   server.on("/", handleRoot);       
   server.on("/power_on", handlePowerOn);       
   server.on("/force_power_off", handleForcePowerOff);       
   server.onNotFound(handleNotFound);  
   Serial.println("HTTP server started");//  告知用户ESP8266网络服务功能已经启动
}

void loop(void){
  server.handleClient(); 
}
                                                                          
void handleRoot() {
  server.send(200, "text/html; charset=utf-8", " \
  <a href=\"/power_on\"  target=\"opentype\">开机</a>\r\n \
  <br> \
  <br> \
  <a href=\"/force_power_off\"  target=\"opentype\">强制关机</a>\r\n \
  ");   
}
 
void handleNotFound(){                                        
  server.send(404, "text/plain", "404: Not found");   
}

void handlePowerOn() {  
  server.send(200, "text/html; charset=utf-8", " \
  <p>正在执行开机</p>\r\n \
  <br> \
  <br> \
  <a href=\"/\"  target=\"opentype\">返回</a>\r\n \
  "); 
  digitalWrite(RELAY_PIN, HIGH);
  delay(500);
  digitalWrite(RELAY_PIN, LOW);
}

void handleForcePowerOff() {  
  server.send(200, "text/html; charset=utf-8", " \
  <p>正在执行强制关机，需要12s</p>\r\n \
  <br> \
  <br> \
  <a href=\"/\"  target=\"opentype\">返回</a>\r\n \
  ");
  digitalWrite(RELAY_PIN, HIGH);
  delay(12000);
  digitalWrite(RELAY_PIN, LOW);
}